package com.tattermedia.allthataudio.views;

import hw.ats.news.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.tattermedia.allthataudio.audio.AudioPlayerManager;
import com.tattermedia.allthataudio.misc.TimeFomatter;


/**
 * BGM Player View
 */
public class AudioPlayerProgressView extends View {
	
	public static final int		PROGRESS_COLOR_BACKGROUND = Color.WHITE;
	public static final int		PROGRESS_COLOR_BUFFERING = Color.GRAY;
	public static final int		PROGRESS_COLOR_PLAYED = Color.argb(0xff, 0xff, 0xcc, 0x66);
	
	public static final int 	SEEK_BUTTON_COLOR = Color.argb(0xff, 0xff, 0x66, 0x99);
	
	public static final int		PROGRESS_HEIGHT = 8;
	public static final int		SEEK_BUTTON_WIDTH = 10;
	public static final int		SEEK_BUTTON_HEIGHT = 20;
	
	private Paint	mPaint;
	
	private int mCurrentPosition;
	private int mTotalDuration;
	private int mDownloadPercent;
	private int mDownloadBarWidth;
	
	private Rect mSeekButtonRect;
	
	private int mProgressWidth;
	private int mProgressStart;
	
	
	private boolean mIsSeek;
	
	
	public AudioPlayerProgressView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mPaint = new Paint();
		mPaint.setAlpha(0xff);
		mIsSeek = false;
	}
	
	
	@Override
	protected void onDraw(Canvas canvas) {	
		
		AudioPlayerManager manager = AudioPlayerManager.getManager();
		SharedBitmap sharedBitmap = SharedBitmap.getInstance();
		
		if (!mIsSeek) {
			mCurrentPosition = manager.getCurrentPosition();
		}
		
		
		mTotalDuration = manager.getTotalDuration();
		mDownloadPercent = manager.getCurrentDownloadPercent();
		
		Rect bounds = new Rect();
		getDrawingRect(bounds);
		
		mProgressWidth = bounds.width() - SEEK_BUTTON_WIDTH;
		mProgressStart = bounds.left + SEEK_BUTTON_WIDTH / 2;
		
		int centerY = bounds.top + SEEK_BUTTON_HEIGHT/2 + 2;//bounds.centerY();
		
		
		// draw progress background
//		Rect tempRect = new Rect(mProgressStart, centerY - PROGRESS_HEIGHT/2, mProgressStart + mProgressWidth, centerY + PROGRESS_HEIGHT/2);
//		mPaint.setColor(PROGRESS_COLOR_BACKGROUND);
//		mPaint.setStyle(Paint.Style.FILL);
//		canvas.drawRect(tempRect, mPaint);
		
		
		drawBar(canvas, mProgressStart, centerY - PROGRESS_HEIGHT/2, 
				mProgressWidth, 
				sharedBitmap.getBitmap(R.drawable.seekbar_background_left),
				sharedBitmap.getBitmap(R.drawable.seekbar_background_middle),
				sharedBitmap.getBitmap(R.drawable.seekbar_background_right));
		
		
//		// draw buffering
		mDownloadBarWidth = mDownloadPercent == 0 ? 0 : mProgressWidth * mDownloadPercent / 100;
//		tempRect = new Rect(mProgressStart, centerY - PROGRESS_HEIGHT/2, mProgressStart + mDownloadBarWidth, centerY + PROGRESS_HEIGHT/2);
//		mPaint.setColor(PROGRESS_COLOR_BUFFERING);
//		mPaint.setStyle(Paint.Style.FILL);
//		canvas.drawRect(tempRect, mPaint);
		drawBar(canvas, mProgressStart, centerY - PROGRESS_HEIGHT/2, 
				mDownloadBarWidth,  
				sharedBitmap.getBitmap(R.drawable.seekbar_buffered_left),
				sharedBitmap.getBitmap(R.drawable.seekbar_buffered_middle),
				mDownloadBarWidth > mProgressWidth - 4 ? sharedBitmap.getBitmap(R.drawable.seekbar_buffered_right) : null);

//		//draw played
		int playedBarWidth = mCurrentPosition == 0 ? 0 : (mProgressWidth - SEEK_BUTTON_WIDTH) * mCurrentPosition / mTotalDuration;
//		tempRect = new Rect(mProgressStart, centerY - PROGRESS_HEIGHT/2, mProgressStart + playedBarWidth, centerY + PROGRESS_HEIGHT/2);
//		mPaint.setColor(PROGRESS_COLOR_PLAYED);
//		mPaint.setStyle(Paint.Style.FILL);
//		canvas.drawRect(tempRect, mPaint);
		
		drawBar(canvas, mProgressStart, centerY - PROGRESS_HEIGHT/2, 
				playedBarWidth,  
				sharedBitmap.getBitmap(R.drawable.seekbar_played_left),
				sharedBitmap.getBitmap(R.drawable.seekbar_played_middle),
				null);
		
		// seek button
		int seekPosition = mProgressStart + playedBarWidth;
		
		if (seekPosition > mProgressStart + mProgressWidth - SEEK_BUTTON_WIDTH) {
			seekPosition = mProgressStart + mProgressWidth - SEEK_BUTTON_WIDTH;
		}
		
		mSeekButtonRect = new Rect(seekPosition, centerY - SEEK_BUTTON_HEIGHT/2, seekPosition + SEEK_BUTTON_WIDTH, centerY + SEEK_BUTTON_HEIGHT/2);
		mPaint.setColor(SEEK_BUTTON_COLOR);
		mPaint.setStyle(Paint.Style.FILL);
		canvas.drawRect(mSeekButtonRect, mPaint);
		
		mPaint.setColor(Color.BLACK);
		mPaint.setStyle(Paint.Style.STROKE);
		canvas.drawRect(mSeekButtonRect, mPaint);
		
	}
	
	protected void drawBar(Canvas canvas, int x, int y, int width, Bitmap left, Bitmap middle, Bitmap right) {
		
		assert(left != null);
		canvas.drawBitmap(left, x, y, mPaint);
		
		x += left.getWidth();
		width -= left.getWidth();
		
		int dstWidth = width - (right != null ? left.getWidth() : 0);
		Rect srcRect = new Rect(0, 0, middle.getWidth(), middle.getHeight());
		Rect dstRect = new Rect(x, y, x + dstWidth, y + middle.getHeight());
		canvas.drawBitmap(middle, srcRect, dstRect, mPaint);
		
		if (right != null) {
			x += dstWidth;
			
			canvas.drawBitmap(right, x, y, mPaint);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		Rect bounds = new Rect();
		getDrawingRect(bounds);
		
		int x = (int) event.getX();
		int y = (int) event.getY();
		
		x -= bounds.left;
		y -= bounds.top;
		
		float value = (float)(x - mProgressStart) / (mProgressWidth - SEEK_BUTTON_WIDTH);
		
		if (value * 100 > mDownloadPercent) {
			value = mDownloadPercent / 100.0f;
		}
		
		int position = (int) (mTotalDuration * value);
		
		if (position < 0) {
			position = 0;
		} else if (position > mTotalDuration) {
			position = mTotalDuration - 1000;
		}
		
		if (event.getAction() == MotionEvent.ACTION_DOWN ||
				event.getAction() == MotionEvent.ACTION_MOVE) {
			
			mIsSeek = true;		
			mCurrentPosition = position;
			invalidate();
					
			return true;
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			
			AudioPlayerManager.getManager().seekTo(mCurrentPosition);
			mIsSeek = false;
			
			invalidate();
			return true;
		}

		
		
		return super.onTouchEvent(event);
	}



}
